<?php
class sqlq
{
	protected $_dbHandler;
	protected $_result;
	function connect($address,$user,$pass,$db)
	{
		$this->_dbHandler = new mysqli($address,$user,$pass,$db);
		if($this->_dbHandler->errno)
			return 0;
		else
			return 1;
	}
	
	function disconnect()
	{
		if(!$this->_dbHandler->errno)
			$this->_dbHandler->close();
	}
	
	function selectAll()
	{
		$query = 'select * from `'.$this->_table.'`'; // trong file model.class.php
		return $this->query($query);
	}
	
	function query($query,$singleRecord=0)
	{
		if($this->_dbHandler->connect_error)
		{
			//die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
			echo 'Lỗi';
		}
		$this->_result = $this->_dbHandler->query($query);
		//$this->_result = mysql_query($query, $this->_dbHandler);
		/* $result = array();
		$table = array();
		$field = array();
		$tempResult  = array();
		
		$numOfFields =$this->_result->field_count;
		if(preg_match('/select/i', $query))
		{
			for($i=0;$i<$numOfFields;$i++)
			{
				array_push($table, mysql_field_table($this->_result, $i)); //SELECT table1.field1 , table1.field2, table2.field3, table2.field4 FROM table1,table2 
				array_push($field, mysql_field_name($this->_result, $i));
				
			}
		}
		
		while($row =$this->_result->fetch_array())
		{
			for ($i = 0;$i < $numOfFields; ++$i) {
				$tempResults[$table[$i]][$field[$i]] = $row[$i];
			}
			if ($singleRecord == 1) {
				mysql_free_result($this->_result);
				return $tempResults;
			}
			array_push($result,$tempResults);
		}
		mysql_free_result($this->_result); 
		
		return($result);*/
		return $this->_result;
	}
}